package net.hn.hnms.sys.system.service.impl;

import cn.hutool.http.Method;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import net.hn.hnms.sys.system.service.IhnService;
import net.hn.hnms.sys.system.utils.IhnUtils;
import org.springframework.stereotype.Service;

import java.io.PrintStream;
import java.net.HttpCookie;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class IhnServiceImpl implements IhnService {
//    @Override
//    public String gettoken() {
//        cn.hutool.http.HttpRequest httpRequest = new cn.hutool.http.HttpRequest(IhnUtils.IHN_URL+IhnUtils.GETTOKEN+"?corpid=ww6b15baa92b357a06&corpsecret=G44esWi2mImo3Nsi5pQUrh5WsBpRY1dyOJLfiQ746A8&agentid=1000569");
//        log.error("gettoken=="+IhnUtils.IHN_URL+IhnUtils.GETTOKEN+"?corpid=ww6b15baa92b357a06&corpsecret=G44esWi2mImo3Nsi5pQUrh5WsBpRY1dyOJLfiQ746A8&agentid=1000569");
//        httpRequest.setMethod(Method.GET);
//        try {
//            cn.hutool.http.HttpResponse execute = httpRequest.execute();
//            boolean ok = execute.isOk();
//            System.out.println(ok);
//            List<HttpCookie> cookies = execute.getCookies();
//            PrintStream var10001 = System.out;
//            Objects.requireNonNull(var10001);
//            cookies.forEach(var10001::println);
//            String s = execute.body();
//            if (Func.isNotEmpty(s)){
//                System.out.println(s);
//                JSONObject jsonObject = JSONObject.parseObject(s);
//                int errcode = jsonObject.getIntValue("errcode");
//                if (errcode == 0){
//                    String access_token = jsonObject.getString("access_token");
//                    IhnUtils.ACCESS_TOKEN = access_token;
//                }
//
//                return IhnUtils.ACCESS_TOKEN;
//            }else{
//                return null;
//            }
//
//        } catch (Exception e) {
//            System.out.println("网络请求异常:" + e.getMessage());
//            e.printStackTrace();
//            throw e;
//        }
//    }

    @Override
    public String userSimplelist(String department_id,String fetch_child) {
        cn.hutool.http.HttpRequest httpRequest = new cn.hutool.http.HttpRequest(IhnUtils.IHN_URL+IhnUtils.IHNUSERINFO_ALL_BY_DEPTID+"?deptId="+department_id);
        log.error("userSimplelist=="+IhnUtils.IHN_URL+IhnUtils.IHNUSERINFO_ALL_BY_DEPTID+"?deptId="+department_id);
//        httpRequest.setMethod(Method.GET);
        httpRequest.setMethod(Method.POST);
        try {
            cn.hutool.http.HttpResponse execute = httpRequest.execute();
            boolean ok = execute.isOk();
            System.out.println(ok);
            List<HttpCookie> cookies = execute.getCookies();
            PrintStream var10001 = System.out;
            Objects.requireNonNull(var10001);
            cookies.forEach(var10001::println);
            String body = execute.body();
            System.out.println(body);
            return body;
        } catch (Exception e) {
            System.out.println("网络请求异常:" + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }

    @Override
    public String ihndeptList(String id, String no_fetch_child, String get_party_path) {
        cn.hutool.http.HttpRequest httpRequest = new cn.hutool.http.HttpRequest(IhnUtils.IHN_URL+IhnUtils.IHNDEPT_LIST+"?id=6771");
        log.error("departmentlist=="+IhnUtils.IHN_URL+IhnUtils.IHNDEPT_LIST+"?id="+id);
        httpRequest.setMethod(Method.GET);
        try {
            cn.hutool.http.HttpResponse execute = httpRequest.execute();
            boolean ok = execute.isOk();
            System.out.println(ok);
            List<HttpCookie> cookies = execute.getCookies();
            PrintStream var10001 = System.out;
            Objects.requireNonNull(var10001);
            cookies.forEach(var10001::println);
            String body = execute.body();
            System.out.println(body);
            return body;
        } catch (Exception e) {
            System.out.println("网络请求异常:" + e.getMessage());
            e.printStackTrace();
            throw e;
        }
    }

//    @Override
//    public String userGet(String userid) {
//        if (Func.isNotEmpty(IhnUtils.ACCESS_TOKEN)){
//
//        }else{
//            IhnUtils.ACCESS_TOKEN=gettoken();
//        }
//        cn.hutool.http.HttpRequest httpRequest = new cn.hutool.http.HttpRequest(IhnUtils.IHN_URL+IhnUtils.USER_GET+"?access_token="+IhnUtils.ACCESS_TOKEN+"&userid="+userid);
//        log.error("userGet=="+IhnUtils.IHN_URL+IhnUtils.USER_GET+"?access_token="+IhnUtils.ACCESS_TOKEN+"&userid="+userid);
//        httpRequest.setMethod(Method.GET);
//        try {
//            cn.hutool.http.HttpResponse execute = httpRequest.execute();
//            boolean ok = execute.isOk();
//            System.out.println(ok);
//            List<HttpCookie> cookies = execute.getCookies();
//            PrintStream var10001 = System.out;
//            Objects.requireNonNull(var10001);
//            cookies.forEach(var10001::println);
//            String body = execute.body();
//            System.out.println(body);
//            return body;
//        } catch (Exception e) {
//            System.out.println("网络请求异常:" + e.getMessage());
//            e.printStackTrace();
//            throw e;
//        }
//    }
}
